package git.jas0nch.boottest.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * projectName : boottest
 * className: DruidConfig
 * description: TODO 类描述
 *
 * @author : git.jas0nch
 * date: 2022-02-19
 */

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }

    // 后台监控

    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        // 后台登录的账号密码配置：
        HashMap<String, String> initParameter = new HashMap<>();

        initParameter.put("loginUsername", "admin");
        initParameter.put("loginPassword", "123456");
        // permit
        initParameter.put("allow", "");
        // forbid
        initParameter.put("user", "ip_url");
        bean.setInitParameters(initParameter);
        return bean;
    }

}
